<?php
class PlantsController
{
    public $player;

    public $loaded = false;
    public $changed = false;
    public $plants = false;
    //$plants[TILE] = array(ID,END_TIME,LAST_WATER);

    public $header = array(8,8,28,28);

    public $time_start = 1297160000;

    function  __construct($player) {
        $this->player = $player;
    }

    // set to grow
    public function plant($tile,$id)
    {
        $this->load();
        log::debug('planting','tile='.$tile.' id='.$id);
        if(isset($this->plants[$tile]))
        {
            log::debug('planting','already exists');
            return false;
        }
        // TODO check for existing in library
        // TODO get time
        $grow_time = 60*60;
        $grow_time = rpc::$time + $grow_time;
        $this->plants[$tile] = array($id, $grow_time);
        $this->changed = true;
        return true;
    }

    public function save()
    {
        if($this->changed)
        {
            log::debug('saving plants');
            $string = $this->parse();
            $string_blob = $this->parse_blob();
            $query = 'INSERT INTO `players_plants` SET
`user_id`='.$this->player->user_id.',
`plants_blob`=\''.$string_blob.'\'
ON DUPLICATE KEY UPDATE
`user_id`='.$this->player->user_id.',
`plants_blob`=\''.$string_blob.'\'';
            return _database::query($query);
        }
        return false;
    }

    // out for client
    public function getAll()
    {
        $this->load();
        return $this->plants;
    }

    // loading from database
    public function load()
    {
        if($this->loaded) return true;
        $query = 'SELECT `plants_blob` FROM `players_plants` WHERE `user_id`='.$this->player->user_id;

        $data = _database::torow($query);
        if($data && is_array($data) && $data['plants_blob'])
        {
            $this->plants = $this->unparse_blob($data['plants_blob']);
        }
        else
        {
            $this->plants = array();
        }
        log::debug('player plants', $this->plants);
        $this->loaded = true;
        return true;
    }
    public function unparse_blob($string)
    {
        log::debug('unparsing blob');
        $plants = array();
        if($string)
        {
            $arr = blob_decode($string,$this->header);
            foreach($arr as $row)
            {
                list($tile,$id,$endtime,$lastwater) = array($row[0],$row[1],$row[2],$row[3]);
                $endtime = (int)$endtime + $this->time_start;
                $lastwater = (int)$lastwater + $this->time_start;
                if((int)$tile)
                $plants[(int)$tile] = array((int)$id,(int)$endtime,(int)$lastwater);
            }
        }
        log::debug($plants,'unparsed blob');
        return $plants;
    }

    public function parse_blob()
    {
        $this->load();
        if(!is_array($this->plants) || !count($this->plants)) return '';
        $out = null;
        foreach($this->plants as $tile => $data)
        {
            $data[1] = max((int)$data[1],$this->time_start);
            $data[2] = max((int)$data[2],$this->time_start);
            $out[] = array($tile,$data[0],($data[1] - $this->time_start),($data[2] - $this->time_start));
        }
        log::debug('parsed plants',$out);
        return blob_encode($out,$this->header);
    }
}
?>
